/**
 * Copyright (c) 2015-2018, Golly Hu 胡高 (3722711@qq.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.hg.jfsa.manage.controller.system;

import java.io.File;
import java.util.List;
import java.util.Map;

import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.kit.Kv;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.StrKit;

import cn.hg.base.jfinal.controller.BaseController;
import cn.hg.base.kit.UploadFileKit;
import cn.hg.jfsa.common.JfsaConstants;
import cn.hg.jfsa.manage.controller.system.validator.FileValidator;
import cn.hg.jfsa.manage.service.JfsaFileService;
import cn.hg.jfsa.model.JfsaFile;

/**
 * @author Golly
 */
public class JfsaFilesController extends BaseController {

	private static String UPLOAD_PATH = PathKit.getWebRootPath() + JfsaConstants.JFSA_UPLOAD_PATH;
	@Inject
	private JfsaFileService fileService;

	public void ajaxDel () {
		String folder = this.getPara("folder");
		String[] fileNameArray = this.getParaValues("files[]");
		String tip = "";

		for (String fileName : fileNameArray) {
			File f = new File(JfsaFilesController.UPLOAD_PATH + folder + "/" + fileName);

			if (!f.exists()) {
				continue;
			}

			if (f.isDirectory()) {

				if (f.listFiles().length == 0) {
					f.delete();
				} else {
					tip += " [" + fileName + "] ";
				}

			} else if (f.isFile()) {
				f.delete();
			}

		}

		if (StrKit.notBlank(tip)) {
			tip = "文件夹" + tip + "下还存在内容，并没有被删除！";
		}

		Kv kv = Kv.by("tip", tip);
		this.renderAjaxSuccessed(kv);
	}

	@Before (FileValidator.class)
	public void ajaxNewFolder () {
		String folder = this.getPara("folder");
		String newFolder = this.getPara("folderName");
		String newPath = JfsaFilesController.UPLOAD_PATH + folder + "/" + newFolder;
		File file = new File(newPath);

		if (file.exists()) {
			this.renderAjaxFailed("1", "文件夹已经存在或与现有文件重名！");
			return;
		}

		file.mkdirs();
		this.renderAjaxSuccessed();
	}

	public void ajaxUpload () {
		this.getFiles();
		String folder = this.getPara("folder");
		List<Map<String, String>> list = UploadFileKit.uploadFile(folder, this);
		Kv kv = Kv.by("count", list.size());

		for (Map<String, String> uploaded : list) {
			String key = uploaded.keySet().iterator().next();
			kv.set(key, uploaded.get(key));
		}

		this.renderAjaxSuccessed(kv);
	}

	/**
	 *
	 */
	public void getList () {
		String folder = this.getPara("folder", "");
		String action = this.getPara("action", "go");
		String accept = this.getPara("accept", "");

		switch (action) {
			case "go":
			case "reload":
				break;

			case "back":
				if (StrKit.notBlank(folder) && folder.startsWith("/")) {
					folder = folder.substring(0, folder.lastIndexOf("/"));
				}
		}

		this.setAttr("folder", folder);
		this.setAttr("action", action);
		this.setAttr("accept", accept);

		if (StrKit.isBlank(folder)) {
			this.setAttr("folders", new String[] {
				"/"
			});
		} else {
			String[] folders = folder.substring(1).split("/");
			this.setAttr("folders", folders);
		}

		String realFolder = JfsaFilesController.UPLOAD_PATH + folder;
		File path = new File(realFolder);

		if (null == path || !path.exists() || !path.isDirectory()) {
			this.render("folder_not_exist.html");
			return;
		}

		List<JfsaFile> files = this.fileService.getFile(realFolder);
		this.setAttr("files", files);
		this.render("file_list.html");
	}

	/*
	 * (non-Javadoc)
	 * @see cn.hg.base.jfinal.controller.BaseController#index()
	 */
	@Override
	public void index () {
		this.keepPara();
		this.set("UPLOAD_PATH", JfsaConstants.JFSA_UPLOAD_PATH);
	}

}
